package com.mg.game.content.tasksscreen;

import android.graphics.Canvas;
import android.view.MotionEvent;

import com.mg.framework.kernel.Screen;
import com.mg.framework.kernel.Touchable;
import com.mg.game.content.ConstantString;
import com.mg.game.content.TankGameAssets;

public class TasksScreen extends Screen implements Touchable {

	private TaskManager taskManager;
	private int startIndex;
	private int endIndex;
	private Task[] tasks;
	private Left leftIcon;
	private Right rightIcon;
	private Return returnIcon;
	private boolean change;

	public void setTaskManager(TaskManager taskManager) {
		this.taskManager = taskManager;
		this.tasks = this.taskManager.getTask(startIndex, endIndex);
	}

	public void setStartIndexAndEndIndex(int offset) {
		if (this.startIndex == 0 && Math.signum(offset) == -1) {
			return;
		}
		if (this.endIndex == (ConstantString.TASKCOUNT - 1)
				&& Math.signum(offset) == 1) {
			return;
		}
		this.change = true;
		this.startIndex += offset;
		this.endIndex += offset;
	}

	public TasksScreen(int width, int height) {
		super(width, height);
		this.leftIcon = new Left(this, TankGameAssets.leftIcon);
		this.rightIcon = new Right(this, TankGameAssets.rightIcon);
		this.returnIcon = new Return(this, TankGameAssets.returnIcon);
		this.startIndex = 0;
		this.endIndex = 14;
	}

	@Override
	protected void gameUpdate() {
		if (this.change) {
			this.tasks = this.taskManager.getTask(startIndex, endIndex);
			this.change = false;
		}
	}

	@Override
	protected void screenUpdate(Canvas canvas) {
		this.leftIcon.screenUpdate(canvas);
		this.rightIcon.screenUpdate(canvas);
		this.returnIcon.screenUpdate(canvas);
		for (int i = 0; i < tasks.length; i++) {
			tasks[i].screenUpdate(canvas);
		}
	}

	@Override
	public void onTouchEvent(MotionEvent event) {
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			dispenseToActor(leftIcon, event);
			dispenseToActor(rightIcon, event);
			dispenseToActor(returnIcon, event);
			for (int i = 0; i < this.tasks.length; i++) {
				dispenseToActor(this.tasks[i], event);
			}
			break;
		}
	}

	@Override
	public boolean isTouched(MotionEvent event) {
		return true;
	}
}
